﻿using ExpenseSystem.Common;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Core.EmployeeModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using System.Collections.Generic;
using System.Web.Http;
using System.Linq;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Employee")]
    public class EmployeeController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;

        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        UserProfile userProfile;
        public EmployeeController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
            userProfile = Authority.GetUserProfile();
        }
        EmployeeFetcher fetcher = new EmployeeFetcher();
        EmployeeManager manager = EmployeeManager.CreateInstance();

        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(fetcher, "ORG_EMPLOYEE_LIST", paramModels, getInhibition);
        }

        [Route("ListByTitleType")]
        [HttpGet]
        public ResponseModel ListByTitleType(long type)//0=销售,1=设计,2=安装,3=搬运,DEFAULT=其他
        {
            TitleFetcher titleFetcher = new TitleFetcher();
            UserProfile userProfile = Authority.GetUserProfile();
            string message;
            int rows;
            IList<TitleModel> titleModels = titleFetcher.List(userProfile.SiteId, Lor.OrderBy.GetDefault(), Lor.Limit.GetDefault(),out message, out rows);
            IEnumerable<long> titles = null;
            switch (type)
            {
                case 0:
                    titles = from title in titleModels where title.Sales  select title.AutoId;
                    break;
                case 1:
                    titles = from title in titleModels where title.Designer select title.AutoId;
                    break;
                case 2:
                    titles = from title in titleModels where title.Service select title.AutoId;
                    break;
                case 3:
                    titles = from title in titleModels where title.Carrier select title.AutoId;
                    break;
                default:
                    titles = from title in titleModels select title.AutoId;
                    break;
            }
            Lor.Restrain restrain = Lor.Restrain.In("TitleId",titles);
            return NormalControllerHelper.List(fetcher, "ORG_EMPLOYEE_LIST",new Lor.Restrain[] { restrain },null, getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "ORG_EMPLOYEE_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(EmployeeModel data)
        {
            return NormalControllerHelper.Set(fetcher, "ORG_EMPLOYEE_SET", data, manager, setInhibition, getInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(EmployeeModel data)
        {
            return NormalControllerHelper.Remove("ORG_EMPLOYEE_DEL", data, manager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("ORG_EMPLOYEE_DEL", id, manager);
        }
    }
}
